﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BYFW.BOL;
using System.Data;
using BYFW.SFL;

namespace LDSP.BRL
{
    public class EnterpriseConfig : BaseEntity
    {
        public EnterpriseConfig(int enterpriseID)
            : this()
        {
            m_enterpriseID = enterpriseID;
        }
        protected EnterpriseConfig()
        {
            m_tableName = "t_enterpriseConfig";
            m_hasComment = true;
            m_hasName = true;
            m_tblCacheManager.IsEnable = true;
        }

        protected int m_enterpriseID = 0;

        /// <summary>
        /// 企业ID
        /// </summary>
        public int EnterpriseID
        {
            get { return m_enterpriseID; }
        }

        protected override void FillContainer()
        {
            base.FillContainer();
            m_container.Add("enterpriseID", m_enterpriseID);
        }

        public override bool GetProperties(System.Data.DataRow record)
        {
            if (!base.GetProperties(record))
            {
                return false;
            }
            m_enterpriseID = this.GetIntValue(record, "enterpriseID");
            return true;
        }

        /// <summary>
        /// 获取指定的Config，若其不存在，则使用默认值创建一个
        /// </summary>
        /// <param name="enterpriseID">企业的ID</param>
        /// <param name="code">指定的Config名</param>
        /// <param name="createNew">当config不存在时，则创建一个</param>
        /// <param name="dft">当config不存在时，使用此默认值创建一个</param>
        /// <returns></returns>
        protected EnterpriseConfig GetConfig(string name, bool createNew = true, object dft = null)
        {
            string cond = string.Format("where {0}={1} and {2}={3}",
                this.DB.GetCol("enterpriseID"),
                this.DB.GetVal(m_enterpriseID),
                this.DB.GetCol("name"),
                this.DB.GetVal(name));
            DataTable dt = this.GetRecords(cond, true);
            EnterpriseConfig tmp = new EnterpriseConfig();
            if (dt.Rows.Count > 0)
            {
                tmp.GetProperties(dt.Rows[0]);
            }
            else if (createNew)
            {
                tmp.m_enterpriseID = m_enterpriseID;
                tmp.Name = name;
                if (dft != null)
                {
                    tmp.Comment = dft.ToString();
                }
                tmp.Add();
            }
            return tmp;
        }

        /// <summary>
        /// 移除指定的Config
        /// </summary>
        /// <param name="enterpriseID">企业的ID</param>
        /// <param name="code">指定的Config名</param>
        public void RemoveConfig(string name)
        {
            EnterpriseConfig tmp = this.GetConfig(name, false);
            tmp.Delete();
        }

        #region System Config

        /// <summary>
        /// 实施措施名
        /// </summary>
        public string ActionFieldName
        {
            get
            {
                string res = GetConfig("ActionFieldName").Comment;
                return string.IsNullOrEmpty(res) ? "实施措施" : res;
            }
            set
            {
                if (this.ActionFieldName != value)
                {
                    EnterpriseConfig tmp = GetConfig("ActionFieldName");
                    tmp.Comment = value;
                    tmp.Update();
                }
            }
        }
        /// <summary>
        /// 自我评估名
        /// </summary>
        public string SelfEvaluationFieldName
        {
            get
            {
                string res = GetConfig("SelfEvaluationFieldName").Comment;
                return string.IsNullOrEmpty(res) ? "自我评估" : res;
            }
            set
            {
                if (this.SelfEvaluationFieldName != value)
                {
                    EnterpriseConfig tmp = GetConfig("SelfEvaluationFieldName");
                    tmp.Comment = value;
                    tmp.Update();
                }
            }
        }

        /// <summary>
        /// 列表一页显示的行数
        /// </summary>
        public int LineQuantity
        {
            get
            {
                int res = CommonOP.GetInt(GetConfig("LineQuantity").Comment, true);
                return res < 1 ? 10 : res;
            }
            set
            {
                if (this.LineQuantity != value)
                {
                    EnterpriseConfig tmp = GetConfig("LineQuantity");
                    tmp.Comment = value.ToString();
                    tmp.Update();
                }
            }
        }


        /// <summary>
        /// 适用法规定时提醒天数
        /// </summary>
        public int CodificationActiveAlert
        {
            get
            {
                return CommonOP.GetInt(GetConfig("CodificationActiveAlert").Comment, true);
            }
            set
            {
                if (this.LineQuantity != value)
                {
                    EnterpriseConfig tmp = GetConfig("CodificationActiveAlert");
                    tmp.Comment = value.ToString();
                    tmp.Update();
                }
            }
        }

        /// <summary>
        /// 到期未完成任务计划提醒频率(天)
        /// </summary>
        public int TaskPlanAlertDays
        {
            get
            {
                return CommonOP.GetInt(GetConfig("TaskPlanAlertDays").Comment, true);
            }
            set
            {
                if (this.LineQuantity != value)
                {
                    EnterpriseConfig tmp = GetConfig("TaskPlanAlertDays");
                    tmp.Comment = value.ToString();
                    tmp.Update();
                }
            }
        }

        /// <summary>
        /// 到期未完成任务计划提醒类型：所有/仅含指派
        /// </summary>
        public string TaskPlanAlertType
        {
            get
            {
                return GetConfig("TaskPlanAlertType").Comment;
            }
            set
            {
                if (this.TaskPlanAlertType != value)
                {
                    EnterpriseConfig tmp = GetConfig("TaskPlanAlertType");
                    tmp.Comment = value.ToString();
                    tmp.Update();
                }
            }
        }

        /// <summary>
        /// 适用法规模板名
        /// </summary>
        public string FavoriteCodificationTemplateName
        {
            get
            {
                return GetConfig("FavoriteCodificationTemplateName").Comment;
            }
            set
            {
                if (this.FavoriteCodificationTemplateName != value)
                {
                    EnterpriseConfig tmp = GetConfig("FavoriteCodificationTemplateName");
                    tmp.Comment = value.ToString();
                    tmp.Update();
                }
            }
        }

        /// <summary>
        /// 适用标准模板名
        /// </summary>
        public string FavoriteStandardTemplateName
        {
            get
            {
                return GetConfig("FavoriteStandardTemplateName").Comment;
            }
            set
            {
                if (this.FavoriteStandardTemplateName != value)
                {
                    EnterpriseConfig tmp = GetConfig("FavoriteStandardTemplateName");
                    tmp.Comment = value.ToString();
                    tmp.Update();
                }
            }
        }

        /// <summary>
        /// 任务计划模板名
        /// </summary>
        public string TaskPlanTemplateName
        {
            get
            {
                return GetConfig("TaskPlanTemplateName").Comment;
            }
            set
            {
                if (this.TaskPlanTemplateName != value)
                {
                    EnterpriseConfig tmp = GetConfig("TaskPlanTemplateName");
                    tmp.Comment = value.ToString();
                    tmp.Update();
                }
            }
        }
        #endregion
    }
}
